using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;

// this is the "always true" warning
#pragma warning disable 183

namespace LINQ.Prep
{
   [TestFixture]
   public class CondensedPresentation
   {
      [Test]
      public void Demo1_some_definitions()
      {
         var basicArray = new int[0];
         var genericList = new List<int>();
         var genericDictionary = new Dictionary<int, string>();

         Assert.IsTrue(basicArray is IEnumerable);
         Assert.IsTrue(genericList is IEnumerable);
         Assert.IsTrue(genericDictionary is IEnumerable);
         // we're talking about anything that ultimately inherits from IEnumerable

         var ints = new[] {1,2,3,4,5};

         IEnumerable<int> intsGreaterThanOne = ints.Where(x => x > 1);
         IEnumerable<int> intsGreaterThanTwo = intsGreaterThanOne.Where(x => x > 2);
         Assert.AreEqual(3, intsGreaterThanTwo.Count());
         Assert.AreEqual(4, intsGreaterThanOne.Count());
         Assert.AreEqual(5, ints.Count());
         // linq queries are composable

         var predicateWasCalled = false;
         Func<int, bool> predicate = x =>
            {
               predicateWasCalled = true;
               return x > 1;
            };

         intsGreaterThanOne = ints.Where(predicate);
         Assert.IsFalse(predicateWasCalled);
         Assert.AreEqual(4, intsGreaterThanOne.Count());
         Assert.IsTrue(predicateWasCalled);
         // deferred execution
      } // ~ 10-12 minutes

      [Test]
      public void Demo2_paging_with_linq()
      {
         // paging is actually crazy easy in linq.  In fact, the actual sql generated in L2S is very lean and slick
         // paging demonstrates both deferred execution and composable queries

         var ints = new[] {1, 2, 3, 4, 5, 6};
         var baseQuery = ints.Where(x => x > 0);

         var page = 2;
         var pageSize = 3;

         var total = baseQuery.Count();
         var pagedQuery = baseQuery.Skip((page - 1)*pageSize).Take(pageSize);

         Assert.AreEqual(6, total);
         Assert.AreEqual(3, pagedQuery.Count());
         Assert.AreEqual(3, new[] {4, 5, 6}.Union(pagedQuery).Count());
      } // ~ 5 minutes (~ 15-17 minutes total)
   }
}

#pragma warning restore 183
